function Db(ajaxPath,$http){
	this.ajaxPath = ajaxPath + '?c=Ajax';
	this.$http = $http;
	this.idField = '_id';
	this.url = '';
}

Db.prototype._http = function(method,url,data){
	var fd = new FormData();
		for( var attr in data ) {
			fd.append(attr,data[attr] );
		}
		
	var args = {
		method: method,
		url: this.url ? this.url : this.getUrl(url),
		data: fd,
		headers: {'Content-Type': undefined},
		transformRequest: angular.identity
	};
	if ( this.url ) {
		this.url = '';
	}
	return this.$http(args);	
}

Db.prototype.getUrl = function( url ){
	var arr = url.replace(/^\/|\/$/g,'').split("\/");
	if ( arr.length==1 ) {
		return this.ajaxPath + "&a=" + arr[0];
	} else if ( arr.length==2 ) {
		return this.ajaxPath + "&a=" + arr[0] + '&table=' + arr[1];		
	}	
}

Db.prototype.httpMultipart= function (url, method, data) {
	return this._http(method,url,data);
}

//不能上传文件，以json的字符串形式发送到后台
Db.prototype.httpPost = function( url,data ){
	return this._http("post",url, { '_data' : JSON.stringify(data) } )
}

Db.prototype.httpDelete = function( url,data ){
	return this.httpPost(url,data);
}

Db.prototype.httpPut = function( url,data ){
	return this.httpPost(url,data);
}

Db.prototype.httpGet = function( url,options){
	return this.httpPost( url,{"options": options } )
}

//如果conditions为字符串或数字，则{_id:condtions}
//如果为condtions为数组，则{_id:{$in:condtions}}
//如果为对象，则直接将其作为查询条件
Db.prototype.switchBool = function(collection,conditions,field,fn){
	var url = '/switchBool/' + collection;
	return this.httpPost(url,{'_id':conditions,'field':field},fn);
};

//添加数据save采用post方式，对应TP中的add
Db.prototype.save = function(collection,data){
	var url = '/save/' + collection;
	return this._http("post",url, data )
};

//修改整行数据，对应TP中的save
Db.prototype.update = function(collection,data){
	var url = '/update/' + collection;
	return this._http("post",url, data )
};

//修改某些字段值
Db.prototype.modify = function(collection,conditions,data){
	var url = '/modify/' + collection;
	return this.httpPost(url,{'conditions':conditions,'data':data});
};

//根据_id修改字段值
Db.prototype.modifyById = function(collection,data,_id){
	return this.modify(collection,{'_id':_id},data);
};


//根据_id组成的数组修改字段值
Db.prototype.modifyByIds = function(collection,ids,data,fn){
	return this.modify(collection,{'_id':['in',ids]},data);
};


//根据条件查询多条
//params为对象，{  where:{}, field:'', page:''...  }
Db.prototype.find = function(collection,options){
	var url = '/find/' + collection;
	return this.httpGet(url,options);
};

//根据条件查询一条
//params为对象，{  where:{}, field:'', page:''...  }
Db.prototype.findOne = function(collection,options){
	var url = '/findOne/' + collection;
	return this.httpGet(url,options);
};

//根据条件查询个数 {where:{}}
Db.prototype.count = function(collection,where,group){
	var url = '/count/' + collection ;
	if ( group ) {
		return this.httpGet(url,{'where':where,'group':group });
	} else {
		return this.httpGet(url,{'where':where});
	}
	
};

//根据id查询一条
Db.prototype.findById = function(collection,_id){
	return this.findOne( collection,{'_id':_id});
};


//根据_id查询一个文档的指定字段
Db.prototype.findFieldsById = function(collection,_id,projection){
	return this.findOne( collection,{'_id':_id,'field':projection} );
};

//根据条件删除数据
Db.prototype.remove = function(collection,conditions){
	var url = '/remove/' + collection;
	return this.httpDelete(url,{'conditions':conditions});
};

/*
	id可以为单个（字符串、数字），也可以为数组
*/
Db.prototype.removeById = function(collection,id){
	var type = typeof id;
	if (  type === 'string' || type === 'number' ) {
		return this.remove(collection,{'_id':id});
	} else if ( Object.prototype.toString.call(id).slice(8,-1) === 'Array' ) {
		return this.remove(collection,{'_id':['in',id] } );
	}
	
};